package app.redis;

import java.text.MessageFormat;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import app.htby.utils.RedisUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;

/**
 * 单例 Redis
 * 
 * @author lfy.xys
 * @date 2018年6月4日
 *
 */
public class TestRedisClient {

	private static final TestRedisClient rc = new TestRedisClient();
	private static Logger logger = LoggerFactory.getLogger(TestRedisClient.class);

	/** 本地连接 */
	private static Jedis loaclJedis = null;
	/** 大数据连接 */
	private static Jedis dataPlusJedis = null;

	private TestRedisClient() {
	}

	public static TestRedisClient getInstance() {
		
		if (dataPlusJedis == null) {
			dataPlusJedis = new Jedis(RedisUtils.dataPlusIp);
			logger.info("创建了 {} 的Redis.", RedisUtils.dataPlusIp);
		}
		
		if (loaclJedis == null) {
			loaclJedis = new Jedis(RedisUtils.localIp);
			logger.info("创建上 {} 的Redis.", RedisUtils.localIp);
		}
		
		return rc;
	}

	public Jedis getLoaclJedis() {
		return loaclJedis;
	}
	
	public Jedis getDataPlusJedis() {
		return dataPlusJedis;
	}

	public static void main(String[] args) throws Exception {
		
		Jedis jedis3 = new Jedis("localhost");
		System.out.println(jedis3.isConnected());
		jedis3.lpush("listDemo", "1");
		System.out.println(jedis3.isConnected());
		
//		List<String> lrange = jedis3.lrange("listDemo", 0, -1);
//		System.out.println(lrange.size());
//		Thread.sleep(1000);
//		System.out.println(jedis3.isConnected());
//		System.out.println();
		
		
		
//		Jedis jedis4 = new Jedis("asdas");
//		System.out.println(jedis4.isConnected());
//		Thread.sleep(1000);
//		System.out.println(jedis4.isConnected());
		
		
		// 连接本地的 Redis 服务
		// Jedis jedis1 = new Jedis("localhost");
		// System.out.println("连接成功");
		// // 查看服务是否运行
		// System.out.println("服务正在运行: " + jedis1.ping());
		// jedis1.set("runoobkey", "www.runoob.com");
		// System.out.println(jedis1.get("runoobkey"));

//		Jedis jedis2 = RedisClient.getInstance().getLoaclJedis();
//
//		String _key = "test";
//		// 放入集合
//		String[] arr = "AA,AB,AC,AD,AE,AF,AG,BA,BB,BC,BD,BE,BF,BG".split(",");
//		// 设置set
//		// jedis2.sadd(_key, arr);
//		System.out.println("key是否存在：" + jedis2.exists(_key));
//		ScanResult<String> result = jedis2.sscan(_key, "0", new ScanParams().match("*").count(2));
//		String cursor = result.getStringCursor();
//		System.out.println(MessageFormat.format("游标位置:{0}", cursor));
//
//		System.out.println(result.getResult());
//		result = jedis2.sscan(_key, "0", new ScanParams().match("A*"));
//		System.out.println(result.getResult());
//
//		// jedis2.lpush( "listDemo" , arr );
//
//		// 设置list
//		List<String> lrange = loaclJedis.lrange("listDemo", 0, -1);
//
//		System.out.println(lrange.size());
//		// while (true) {
//		try {
//			Thread.sleep(10000);
//		} catch (InterruptedException e) {
//			e.printStackTrace();
//		}
//		// }
//		try {
//			jedis2.lpush("listDemo", "1");
//
//		} catch (Exception e) {
//			System.out.println("没有连接上。	");
//		}
		
		
	}
}
